﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace itau.im.entidade
{
    public class ConversorDeTipos
    {
        public const string MASCARAHORA = "HH:mm:ss";

        public static Int16 ToInt16(object variavel) 
        {
            if (variavel != DBNull.Value 
                && RemoverEspacosEmBranco(variavel) != string.Empty)
            {
                return Convert.ToInt16(variavel);
            }
            else 
            {
                return 0;
            }
        }

        public static Int32 ToInt32(object variavel)
        {
            if (variavel != DBNull.Value
                && RemoverEspacosEmBranco(variavel) != string.Empty)
            {
                return Convert.ToInt32(variavel);
            }
            else
            {
                return 0;
            }
        }

        public static Double ToDouble(object variavel)
        {
            if (variavel != DBNull.Value
                && RemoverEspacosEmBranco(variavel) != string.Empty)
            {
                return Convert.ToDouble(variavel);
            }
            else
            {
                return 0;
            }
        }

        public static DateTime ToDateTime(object variavel)
        {
            if (variavel != DBNull.Value
                && RemoverEspacosEmBranco(variavel) != string.Empty)
            {
                return Convert.ToDateTime(variavel);
            }
            else
            {
                return DateTime.MinValue;
            }
        }

        public static DateTime ToDateTimeParseExact(object variavel, string mascara)
        {
            string variavelSemEspacos = RemoverEspacosEmBranco(variavel);
            if (variavel != DBNull.Value
                && variavelSemEspacos != string.Empty)
            {
                return DateTime.ParseExact(variavelSemEspacos, mascara, null);
            }
            else
            {
                return DateTime.MinValue;
            }
        }

        public static bool ToBoolean(object variavel)
        {
            if (variavel != DBNull.Value
                && RemoverEspacosEmBranco(variavel) != string.Empty)
            {
                return Convert.ToBoolean(variavel);
            }
            else
            {
                return false;
            }
        }

        public static string ToString(object variavel)
        {
            if (variavel != DBNull.Value)
            {
                return Convert.ToString(variavel).Trim();
            }
            else
            {
                return string.Empty;
            }
        }

        public static string RemoverEspacosEmBranco(object valor)
        {
            return ConversorDeTipos.ToString(valor).Trim();
        }

    }
}
